package com.chuyi.demo.swt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

public class TableSample {
	
	
	
	public static void main(String[]args){
		
		
		Display display = Display.getDefault();
		Shell shell = new Shell(display);
		shell.setLayout(new FillLayout());
		shell.setSize(500, 600);
		
		Table table = new Table(shell, SWT.FULL_SELECTION);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		GridData gridData = new GridData();
		gridData.horizontalAlignment=SWT.LEFT;
		gridData.grabExcessHorizontalSpace=true;
		gridData.grabExcessVerticalSpace=true;
		gridData.verticalAlignment=SWT.FILL;
		
		String[]tableHeader={"姓名","性别","电话","电子邮件"};
		for(int i=0;i<tableHeader.length;i++){
			TableColumn tableColumn = new TableColumn(table, SWT.NONE);
			tableColumn.setText(tableHeader[i]);
		}
		
		TableItem item = new TableItem(table, SWT.NONE);
		item.setText(new String[]{"张三","男","123","zhansan@sina.com"});
		
		item = new TableItem(table, SWT.NONE);
		item.setText(new String[]{"李四","男","4528","zhansan@sina.com"});
		
		item = new TableItem(table, SWT.NONE);
		item.setText(new String[]{"周五","女","567","zhansan@sina.com"});
		
		TableItem[] items = table.getItems();
		for(int i=0;i<items.length;i++){
			final TableEditor editor = new TableEditor(table);
			final Text txt = new Text(table, SWT.NONE);
			txt.setText(items[i].getText(0));
			
			editor.grabHorizontal=true;
			editor.setEditor(txt, items[i], 0);
			
			
			txt.addModifyListener(new ModifyListener() {
				@Override
				public void modifyText(ModifyEvent e) {
					editor.getItem().setText(0,txt.getText());
				}
			});
			
			
			final TableEditor editor1 = new TableEditor(table);
			final CCombo combo = new CCombo(table, SWT.NONE);
			combo.add("男");
			combo.add("女");
			combo.setText(items[i].getText(1));
			editor1.grabHorizontal=true;
			editor1.setEditor(combo, items[i], 1);
			combo.addModifyListener(new ModifyListener() {
				
				@Override
				public void modifyText(ModifyEvent e) {
					editor1.getItem().setText(1, combo.getText());
				}
			});
		}
		
		
		
		for(int i=0;i<tableHeader.length;i++){
			table.getColumn(i).pack();
		}
		
		
		shell.pack();
		shell.open();
		while(!shell.isDisposed()){
			if(!display.readAndDispatch()){
				display.sleep();
			}
		}
		display.dispose();
		
	}

}
